using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DbView
{
    /// <summary>
    /// Summary description for SourceSearchForm.
    /// </summary>
    public class SearchSourceForm : System.Windows.Forms.Form
    {
        private SearchSourceParams m_Params;
        //        private string this.m_SearchText;
        ////        private SearchSourceParams.SearchTarget. this.m_SearchTarget;

        private System.Windows.Forms.Button btnOk;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textSearch;
        private RadioButton radioViews;
        private RadioButton radioProcs;
        private RadioButton radioFuncs;
        private GroupBox groupBox1;
        private RadioButton radioTables;
        private RadioButton radioCalcs;
        private RadioButton radioAll;
        private Panel panel1;
        private Label label2;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public SearchSourceForm(SearchSourceParams initialValues)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            this.m_Params = initialValues;
            this.textSearch.Text = this.m_Params.SearchText;
            this.textSearch.SelectAll();

            this.radioViews.Enabled = initialValues.SupportsSearchTarget(SearchSourceParams.SearchTarget.Views);
            this.radioProcs.Enabled = initialValues.SupportsSearchTarget(SearchSourceParams.SearchTarget.Procs);
            this.radioFuncs.Enabled = initialValues.SupportsSearchTarget(SearchSourceParams.SearchTarget.Funcs);
            this.radioTables.Enabled = initialValues.SupportsSearchTarget(SearchSourceParams.SearchTarget.Tables);
            this.radioCalcs.Enabled = initialValues.SupportsSearchTarget(SearchSourceParams.SearchTarget.Calcs);
            this.radioAll.Enabled = initialValues.SupportsSearchTarget(SearchSourceParams.SearchTarget.All);

            // hack for SQLCE. Assume all drivers support searching fields in tables.
            RadioButton r = this.radioTables;

            if (this.m_Params.Target == SearchSourceParams.SearchTarget.Views && this.radioViews.Enabled == true) r = radioViews;
            if (this.m_Params.Target == SearchSourceParams.SearchTarget.Procs && this.radioProcs.Enabled == true) r = radioProcs;
            if (this.m_Params.Target == SearchSourceParams.SearchTarget.Funcs && this.radioFuncs.Enabled == true) r = radioFuncs;
            if (this.m_Params.Target == SearchSourceParams.SearchTarget.Tables && this.radioTables.Enabled == true) r = radioTables;
            if (this.m_Params.Target == SearchSourceParams.SearchTarget.Calcs && this.radioCalcs.Enabled == true) r = radioCalcs;
            if (this.m_Params.Target == SearchSourceParams.SearchTarget.All && this.radioAll.Enabled == true) r = radioAll;
            if (this.m_Params.Target == SearchSourceParams.SearchTarget.Undetermined && this.radioProcs.Enabled == true) r = radioProcs;
            r.Checked = true;

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SearchSourceForm));
            this.btnOk = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.textSearch = new System.Windows.Forms.TextBox();
            this.radioViews = new System.Windows.Forms.RadioButton();
            this.radioProcs = new System.Windows.Forms.RadioButton();
            this.radioFuncs = new System.Windows.Forms.RadioButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioAll = new System.Windows.Forms.RadioButton();
            this.radioCalcs = new System.Windows.Forms.RadioButton();
            this.radioTables = new System.Windows.Forms.RadioButton();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label2 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnOk
            // 
            this.btnOk.Location = new System.Drawing.Point(376, 61);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(75, 23);
            this.btnOk.TabIndex = 3;
            this.btnOk.Text = "OK";
            this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(376, 91);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 4;
            this.btnCancel.Text = "&Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(17, 66);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(72, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "&Search for:";
            // 
            // textSearch
            // 
            this.textSearch.Location = new System.Drawing.Point(81, 64);
            this.textSearch.Name = "textSearch";
            this.textSearch.Size = new System.Drawing.Size(241, 20);
            this.textSearch.TabIndex = 1;
            // 
            // radioViews
            // 
            this.radioViews.AutoSize = true;
            this.radioViews.Location = new System.Drawing.Point(17, 42);
            this.radioViews.Name = "radioViews";
            this.radioViews.Size = new System.Drawing.Size(53, 17);
            this.radioViews.TabIndex = 1;
            this.radioViews.TabStop = true;
            this.radioViews.Text = "&Views";
            this.radioViews.UseVisualStyleBackColor = true;
            // 
            // radioProcs
            // 
            this.radioProcs.AutoSize = true;
            this.radioProcs.Location = new System.Drawing.Point(17, 65);
            this.radioProcs.Name = "radioProcs";
            this.radioProcs.Size = new System.Drawing.Size(79, 17);
            this.radioProcs.TabIndex = 2;
            this.radioProcs.TabStop = true;
            this.radioProcs.Text = "&Procedures";
            this.radioProcs.UseVisualStyleBackColor = true;
            // 
            // radioFuncs
            // 
            this.radioFuncs.AutoSize = true;
            this.radioFuncs.Location = new System.Drawing.Point(17, 88);
            this.radioFuncs.Name = "radioFuncs";
            this.radioFuncs.Size = new System.Drawing.Size(71, 17);
            this.radioFuncs.TabIndex = 3;
            this.radioFuncs.TabStop = true;
            this.radioFuncs.Text = "&Functions";
            this.radioFuncs.UseVisualStyleBackColor = true;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radioAll);
            this.groupBox1.Controls.Add(this.radioCalcs);
            this.groupBox1.Controls.Add(this.radioTables);
            this.groupBox1.Controls.Add(this.radioFuncs);
            this.groupBox1.Controls.Add(this.radioViews);
            this.groupBox1.Controls.Add(this.radioProcs);
            this.groupBox1.Location = new System.Drawing.Point(81, 90);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(241, 164);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Search the source of:";
            // 
            // radioAll
            // 
            this.radioAll.AutoSize = true;
            this.radioAll.Location = new System.Drawing.Point(17, 134);
            this.radioAll.Name = "radioAll";
            this.radioAll.Size = new System.Drawing.Size(73, 17);
            this.radioAll.TabIndex = 5;
            this.radioAll.TabStop = true;
            this.radioAll.Text = "&All Source";
            this.radioAll.UseVisualStyleBackColor = true;
            // 
            // radioCalcs
            // 
            this.radioCalcs.AutoSize = true;
            this.radioCalcs.Location = new System.Drawing.Point(17, 111);
            this.radioCalcs.Name = "radioCalcs";
            this.radioCalcs.Size = new System.Drawing.Size(105, 17);
            this.radioCalcs.TabIndex = 4;
            this.radioCalcs.TabStop = true;
            this.radioCalcs.Text = "&Calculated Fields";
            this.radioCalcs.UseVisualStyleBackColor = true;
            // 
            // radioTables
            // 
            this.radioTables.AutoSize = true;
            this.radioTables.Location = new System.Drawing.Point(17, 19);
            this.radioTables.Name = "radioTables";
            this.radioTables.Size = new System.Drawing.Size(57, 17);
            this.radioTables.TabIndex = 0;
            this.radioTables.TabStop = true;
            this.radioTables.Text = "&Tables";
            this.radioTables.UseVisualStyleBackColor = true;
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.SystemColors.Info;
            this.panel1.Controls.Add(this.label2);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(463, 55);
            this.panel1.TabIndex = 5;
            // 
            // label2
            // 
            this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label2.Location = new System.Drawing.Point(0, 0);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(5);
            this.label2.Size = new System.Drawing.Size(463, 55);
            this.label2.TabIndex = 0;
            this.label2.Text = resources.GetString("label2.Text");
            // 
            // SearchSourceForm
            // 
            this.AcceptButton = this.btnOk;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(463, 284);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.textSearch);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnOk);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "SearchSourceForm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Search Source";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOk_Click(object sender, System.EventArgs e)
        {
            this.m_Params.SearchText = this.textSearch.Text;
            this.m_Params.Target = (this.radioViews.Checked) ? SearchSourceParams.SearchTarget.Views :
                                  (this.radioProcs.Checked) ? SearchSourceParams.SearchTarget.Procs :
                                  (this.radioFuncs.Checked) ? SearchSourceParams.SearchTarget.Funcs :
                                  (this.radioTables.Checked) ? SearchSourceParams.SearchTarget.Tables :
                                  (this.radioCalcs.Checked) ? SearchSourceParams.SearchTarget.Calcs :
                                  (this.radioAll.Checked) ? SearchSourceParams.SearchTarget.All :
                                   SearchSourceParams.SearchTarget.Undetermined;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }

    public class SearchSourceParams
    {
        public enum SearchTarget { Undetermined = 0, Views, Procs, Funcs, Tables, Calcs, JobSteps, All };

        public SearchSourceParams(SearchTarget initialTarget, string searchText, SearchTarget[] SupportedSearches)
        {
            this.m_Target = initialTarget;
            this.m_SearchText = searchText;
            this.m_Supported = SupportedSearches;
        }

        public SearchTarget Target { get { return this.m_Target; } set { this.m_Target = value; } }
        public string SearchText { get { return this.m_SearchText; } set { this.m_SearchText = value; } }
        public bool SupportsSearchTarget(SearchTarget target)
        {
            foreach (SearchTarget tgt in this.m_Supported) if (tgt == target) return true;
            return false;
        }

        SearchTarget m_Target = SearchTarget.Undetermined;
        SearchTarget[] m_Supported;
        string m_SearchText = "";
    }

}
